package com.tbit.uqbike.center.service.impl;

import com.tbit.uqbike.center.constant.SystemData;
import com.tbit.uqbike.center.dao.core.AccountConfigExtDao;
import com.tbit.uqbike.center.dao.core.BatteryModelDetailDao;
import com.tbit.uqbike.center.pojo.AccountConfigExt;
import com.tbit.uqbike.center.pojo.BatteryModel;
import com.tbit.uqbike.center.pojo.BatteryModelDetail;
import com.tbit.uqbike.center.pojo.MachineStatus;
import com.tbit.uqbike.center.service.BatteryModelDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("batteryModelDetailService")
@Slf4j
public class BatteryModelDetailServiceImpl implements BatteryModelDetailService {
    @Autowired
    private BatteryModelDetailDao batteryModelDetailDao;
    @Autowired
    private AccountConfigExtDao accountConfigExtDao;

    @Override
    public List<BatteryModelDetail> getAll() {
        return batteryModelDetailDao.getAll();
    }

    @Override
    public MachineStatus updateMachineBattery(Integer machineId) {
        /**查找设备状态信息*/
        MachineStatus mahcineStatus = SystemData.machineStatusMap.get(machineId);
        if (mahcineStatus != null) {
            /**获取设备电池方案详情*/
            Integer batteryModelId = SystemData.machineIdBatteryModelIdMap.get(machineId);
            if (batteryModelId != null) {
                BatteryModel bm = SystemData.batteryModelMap.get(batteryModelId);
                if (bm != null) {
                    /**BMS电池方案*/
                    if (bm.getBatteryType() == 1 && mahcineStatus.getBmsErr() == 0) {
                        /**更新电量*/
                        if (bm.getMaxMileage() != null && mahcineStatus.getSoc() != null) {
                            mahcineStatus.setSocPercent(mahcineStatus.getSoc());
                            mahcineStatus.setSurplusMileage((int) (bm.getMaxMileage() * (mahcineStatus.getSoc() / 100D)));

                            check(mahcineStatus, new ArrayList<>(), bm);
                        }
                    } else {
                        List<BatteryModelDetail> batteryModelDetails = SystemData.batteryModelListMap.get(batteryModelId);
                        if (batteryModelDetails != null && batteryModelDetails.size() > 0 && mahcineStatus.getBatteryEU() != null) {
                            Float vol = mahcineStatus.getBatteryEU() / 1000F;
                            Boolean update = false;
                            for (BatteryModelDetail batteryModelDetail : batteryModelDetails) {
                                if (batteryModelDetail.getMinVol() <= vol && vol <= batteryModelDetail.getMaxVol()) {
                                    /**更新电量*/
                                    mahcineStatus.setSocPercent(batteryModelDetail.getBatteryPer());
                                    mahcineStatus.setSurplusMileage(batteryModelDetail.getMilage());
                                    update = true;
                                    break;
                                }
                            }
                            check(mahcineStatus, batteryModelDetails, null);
                            /**如果电池方案没有匹配上*/
                            if (!update) {
                                /**更新电量*/
                                mahcineStatus.setSocPercent(-1);
                                mahcineStatus.setSurplusMileage(0);
                            }
                        }
                    }
                }

            }
        }

        return mahcineStatus;
    }


    /**
     * 查询是否开启低电断电,兼容低电断电实际骑行里程不是100%问题
     *
     * @param machineStatus
     */
    public void check(MachineStatus machineStatus, List<BatteryModelDetail> batteryModelDetails, BatteryModel bm) {
        Integer accountId = SystemData.machineIdAccountIdMap.get(machineStatus.getMachineId());
        if (accountId == null) {
            return;
        }

        AccountConfigExt accountConfigExt = SystemData.AccountConfigExtMap.get(accountId);
        if (accountConfigExt != null && Integer.valueOf(accountConfigExt.getParamValue()) > 0) {
            Integer power = Integer.valueOf(accountConfigExt.getParamValue());
            if (!batteryModelDetails.isEmpty()) {
                Integer model = machineStatus.getSocPercent() - power;
                Integer min = 0;

                for (BatteryModelDetail batteryModelDetail : batteryModelDetails) {
                    if (batteryModelDetail.getBatteryPer() < model && min < batteryModelDetail.getBatteryPer()) {
                        min = batteryModelDetail.getBatteryPer();
                        machineStatus.setSurplusMileage(batteryModelDetail.getMilage());
                    }
                }
                if (model <= 0) {
                    machineStatus.setSocPercent(0);
                    machineStatus.setSurplusMileage(0);
                }
            } else {
                machineStatus.setSurplusMileage((int) (bm.getMaxMileage() * ((machineStatus.getSoc() - power) / 100D)));
            }
        }

    }

}  